package ca.clarity.hms.util;

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Description.
 *
 * @author Edward KIM
 * @since 1.0
 */
public class HttpTest {

    public static void main(String[] args) throws IOException {

        String url = "http://webservice.helpmesee.claritydemo.net/HmsAndroidService.asmx/GetClinics";
        HttpTest.testGet(url);
    }


    public static void testPost(String url) throws IOException {

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);

        List params = new ArrayList();
        params.add(new BasicNameValuePair("username", "android"));
        params.add(new BasicNameValuePair("token", "624c5a52-fbfe-40d1-8cd1-d7fae36cb1d8"));


        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);

        httpPost.setEntity(entity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();


        if (httpEntity != null) {
            Log.d("Entity : ", httpEntity.toString());
        }


    }


    public static void testGet(String url) throws IOException {
        HttpClient httpClient = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "WalkingTourCreator/1.0");
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Content-type", "application/json");
        httpGet.setHeader("username", "android");
        httpGet.setHeader("token", "624c5a52-fbfe-40d1-8cd1-d7fae36cb1d8");

        HttpResponse httpResponse = httpClient.execute(httpGet);


        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {
            String res = EntityUtils.toString(entity);

            Log.d("Res : ", res.toString());
        }
    }
}
